package cn.edu.scau.cmi.dengyu.client;

import cn.edu.scau.cmi.dengyu.simpleFactoryMethod.AccidentFactory;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SimpleFactoryMethodClient extends Application {
	final static String[] accidentType = { "司机受伤", "汽车损坏", "人员伤亡", "多种事故" };
	
	private ComboBox<String> cbo = new ComboBox<>();
	private static TextArea taDescription = new TextArea();
	Stage stage = new Stage();
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		GridPane pane0 = new GridPane();
		pane0.setAlignment(Pos.CENTER);
		pane0.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
		pane0.setHgap(5.5);
		pane0.setVgap(5.5);

		cbo.setValue(accidentType[0]);
		ObservableList<String> items = FXCollections.observableArrayList(accidentType);
		cbo.getItems().addAll(items);

		Button btSure = new Button("显示保险信息");
		btSure.setOnAction(e -> setDisplayText(items.indexOf(cbo.getValue())));

		taDescription.setWrapText(true);
		taDescription.setEditable(false);
		pane0.add(new Label("姓名：邓昱"), 0, 0);
		pane0.add(new Label("学号：201627010204"), 1, 0);
		pane0.add(new Label("请选择保险"), 0, 1);
		pane0.add(cbo, 1, 1);
		pane0.add(btSure, 1, 2);
		GridPane.setHalignment(btSure, HPos.RIGHT);

		HBox hBox = new HBox();
		//String path = this.getClass().getClassLoader().getResource(".").getPath();
		//String path = SimpleFactoryMethodClient.class.getClassLoader().getResource(".").getPath();
		//System.out.println(path);
		Image image = new Image("cn/edu/scau/cmi/dengyu/client/pic.jpg");
		
		ImageView imageView = new ImageView(image);
		imageView.setFitHeight(100);
		imageView.setFitWidth(100); 
		imageView.setPreserveRatio(true); 
		hBox.getChildren().add(imageView);
		hBox.getChildren().add(pane0);
		

		VBox vBox = new VBox();
		vBox.getChildren().add(hBox);
		vBox.getChildren().add(taDescription);

		Scene scene = new Scene(vBox);
		primaryStage.setTitle("实验二 简单工厂模式");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public static void setDisplayText(int index) {
		//System.out.println(AccidentFactory.getInstance(accidentType[index]));
		
		taDescription.setText(AccidentFactory.getInstance(accidentType[index]).getContentOfAccident());
	}

	public static void main(String[] args) {
		Application.launch(args);
	}
	

	public void showWindow() throws Exception {
		start(stage);		
	}
}
